package com.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.entity.PageBean;
import com.entity.PurOrder;
import com.entity.Result;
import com.group.PurOrderAddGroup;
import com.service.PurOrderDetailService;
import com.service.PurOrderService;
import com.vo.PurOrderDetailVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/purorderdetail")
public class PurOrderDetailController {
    @Autowired
    private PurOrderDetailService purOrderDetailService;

    @RequestMapping("/getOrderDetailList/{pageIndex}/{pageSize}")
    public Result getOrderDetailList(
          @PathVariable("pageIndex") Integer pageIndex,
          @PathVariable("pageSize") Integer pageSize,
          @RequestBody PurOrderDetailVO purOrderDetailVO
    ){
        PageBean<PurOrder> pageBean = purOrderDetailService.getOrderDetailList(pageIndex, pageSize, purOrderDetailVO);
        return Result.ok(pageBean);
    }

    @GetMapping("/getSumAmount")
    public Result<String> getSumAmount(PurOrder purOrder) {
        //String message = "采购商品总数量："+purOrderDetailService.getSumAmount(purOrder);
        String message = ""+purOrderDetailService.getSumAmount(purOrder);
        return Result.ok(message);
    }

    @GetMapping("/getSumMtsum")
    public Result<String> getSumMtsum(PurOrder purOrder) {
        //String message = "采购商品总金额："+purOrderDetailService.getSumMtsum(purOrder);
        String message = ""+purOrderDetailService.getSumMtsum(purOrder);
        return Result.ok(message);
    }




//
//    @GetMapping("/api/export")
//    public ResponseEntity<byte[]> exportData(HttpServletResponse response) throws IOException {
//        List<PurOrder> dataList = fetchDataFromDatabase(); // 从数据库获取数据
//        Workbook workbook = new XSSFWorkbook();
//        Sheet sheet = workbook.createSheet("Data");
//
//        // 创建表头
//        Row headerRow = sheet.createRow(0);
//        headerRow.createCell(0).setCellValue("日期");
//        headerRow.createCell(1).setCellValue("姓名");
//        headerRow.createCell(2).setCellValue("地址");
//
//        // 填充数据
//        int rowNum = 1;
//        for (PurOrder data : dataList) {
//            Row row = sheet.createRow(rowNum++);
//            row.createCell(0).setCellValue(data.getOrdernum());
//            row.createCell(1).setCellValue(data.getPurGoods().getGname());
//            row.createCell(2).setCellValue(data.getSupplier().getSname());
//        }
//
//        // 设置响应头
//        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");
//
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        workbook.write(baos);
//        workbook.close();
//        byte[] bytes = baos.toByteArray();
//
//        return ResponseEntity
//                .ok()
//                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=data.xlsx")
//                .body(bytes);
//    }
//
//    private List<PurOrder> fetchDataFromDatabase() {
//        // 这里模拟数据库数据
//        List<PurOrder> data = new ArrayList<>();
//        // 添加数据到列表
//        return data;
//    }


//    /**
//     * 批量导出
//     * @param ordernum
//     */
//    @GetMapping("/export")
//    public void exportData(@RequestParam(required = false) String ordernum, HttpServletResponse response) throws IOException {
//        ExcelWriter writer = ExcelUtil.getWriter(true);
//        List<PurOrder> list = new ArrayList<>();
//        //第一种：全部导出(当name为空的时候执行，也就是说前端没有勾选复选框时，全部导出)
////        LambdaQueryWrapper<PurOrder> wrapper = new LambdaQueryWrapper<>();
////        wrapper.like(StringUtils.isNotBlank(ordernum),PurOrder::getOrdernum,ordernum);
//
//        //if(StringUtils.isBlank(name)){
//        //查询所有用户
//        //list = purOrderDetailService.selectOrderDetailListByordernum(ordernum);
//        //}
//        writer.write(list,true);
//
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("用户信息表","UTF-8")+".xlsx");
//        //获取输出流
//        ServletOutputStream outputStream = response.getOutputStream();
//        //将excel写入到输出流里，并设置用完流之后就关闭
//        writer.flush(outputStream,true);
//        //关闭write
//        writer.close();
//    }

}
